package com.example.transaction.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.transaction.common.vo.Result;
import com.example.transaction.sys.entity.Classfy;
import com.example.transaction.sys.entity.User;
import com.example.transaction.sys.service.IClassfyService;
import com.example.transaction.sys.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zh
 * @since 2023-06-20
 */
@RestController
@RequestMapping("/sys/classfy")
public class ClassfyController {

    @Autowired
    private IClassfyService classfyService;

    /**
     * 返回所有类别名
     * @return
     */
    @Cacheable(value = "classfys", key = "'allclassfys'")
    @GetMapping("/all")
    public Result<List<String>> getAllclassfyName(){
        List<String> list=classfyService.listAllClassfyNames();
        return Result.success(list,"查询成功");
    }

    /**
     * 通过类别id查找类别名
     * @param classfyID
     * @return
     */
    @Cacheable(value = "classfys", key = "#classfyID")
    @GetMapping("/classfyID")
    public Result<List<String>> getClassfyNamesByItemID(@RequestParam(value = "classfyID",required = false)Long classfyID) {
        List<String> classfyNames = classfyService.getClassfyNamesByclassfyID(classfyID);
        if (classfyNames != null && !classfyNames.isEmpty()) {
            return Result.success(classfyNames, "查询成功");
        } else {
            return Result.fail("该种类不存在");
        }
    }

    /**
     * 通过类别名查找类别id
     * @param classfyName
     * @return
     */
    @Cacheable(value = "classfys", key = "#classfyName")
    @GetMapping("/classfyName")
    public Result<Integer> getclassfyIDByClassName(@RequestParam(value = "classfyName") String classfyName) {
        Integer classfyID = classfyService.getclassfyIDByClassfyName(classfyName);
        if (classfyID != null) {
            return Result.success(classfyID, "查询成功");
        } else {
            return Result.fail("该种类不存在");
        }
    }

    /*
    新增种类
    */
    @CacheEvict(value = "classfys", allEntries = true)
    @PostMapping
    public Result<String> addRow(@RequestBody Classfy classfy){
        try {
            classfyService.save(classfy);
        }catch (Exception e){
            return Result.fail("种类已存在");
        }
        return Result.success("新增种类成功");
    }

    /**
     * 根据classfyID删除种类
     * @param classfyID
     * @return
     */
    @CacheEvict(value = "classfys", allEntries = true)
    @DeleteMapping("/ID/{classfyID}")
    public Result<Classfy>deleteItemById(@PathVariable("classfyID")Integer classfyID){
        classfyService.removeById(classfyID);
        return Result.success("删除种类成功");
    }

    /**
     * 根据classfyName删除种类
     * @param classfyName
     * @return
     */
    @CacheEvict(value = "classfys", allEntries = true)
    @DeleteMapping("/Name/{classfyName}")
    public Result<Classfy>deleteItemByName(@PathVariable("classfyName")String classfyName){
        LambdaQueryWrapper<Classfy> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(classfyName),Classfy::getClassfyName,classfyName);
        classfyService.remove(wrapper);
        return Result.success("删除种类成功");
    }

    /**
     * 查询所有种类，或者按条件查询（管理员可用
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Cacheable(value = "classfys", key = "'allclassfys'+#classfyName+#pageNo+#pageSize")
    @GetMapping("/list")
    public Result<Map<String,Object>>getUserList(@RequestParam(value = "classfyName",required = false)String classfyName,
                                                 @RequestParam(value = "pageNo")Long pageNo,
                                                 @RequestParam(value = "pageSize")Long pageSize){
        LambdaQueryWrapper<Classfy> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(classfyName),Classfy::getClassfyName,classfyName);     //模糊查询
        Page<Classfy> page=new Page<>(pageNo,pageSize);
        classfyService.page(page,wrapper);
        Map<String,Object>data=new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());
        return Result.success(data);
    }
}
